/**
 * Program  : TabBarActivity.java
 * Author   : qianj
 * Create   : 2012-5-24 下午05:34:19
 *
 * Copyright 2012 by newyulong Technologies Ltd.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of newyulong Technologies Ltd.("Confidential Information").  
 * You shall not disclose such Confidential Information and shall 
 * use it only in accordance with the terms of the license agreement 
 * you entered into with newyulong Technologies Ltd.
 *
 */

package com.newyulong.implatform.activity;

import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

import com.newyulong.implatform.IMPApplication;
import com.newyulong.implatform.R;

/**
 * 拥有tabbar的基本activity
 * 
 * @author qianj
 * @version 1.0.0
 * @2012-5-24 下午05:34:19
 */
public class MainActivity extends ActivityGroup implements
		OnCheckedChangeListener {

	private RadioGroup group;

	public static final String TAB_ITEM_HOME = "home";
	public static final String TAB_ITEM_BUSINESS = "business";
	public static final String TAB_ITEM_APP = "app";
	public static final String TAB_ITEM_SETTING = "setting";

	private static final int LOGOUT = Menu.FIRST;
	private static final int EXIT = Menu.FIRST + 1;
	private static final int ABOUT = Menu.FIRST + 2;

	private TabHost tabHost;

	protected void onCreate(android.os.Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabbar);

		group = (RadioGroup) findViewById(R.id.tabbar_radio_group);
		group.setOnCheckedChangeListener(this);

		tabHost = (TabHost) findViewById(R.id.tabhost);
		tabHost.setup(this.getLocalActivityManager());

		TabSpec tab1 = tabHost.newTabSpec(TAB_ITEM_HOME);
		TabSpec tab2 = tabHost.newTabSpec(TAB_ITEM_BUSINESS);
		TabSpec tab3 = tabHost.newTabSpec(TAB_ITEM_APP);
		TabSpec tab4 = tabHost.newTabSpec(TAB_ITEM_SETTING);

		tab1.setIndicator(TAB_ITEM_HOME).setContent(
				new Intent(this, IndexActivity.class));
		tab2.setIndicator(TAB_ITEM_BUSINESS).setContent(
				new Intent(this, BusinessActivity.class));
		tab3.setIndicator(TAB_ITEM_APP).setContent(
				new Intent(this, AppMarketActivity.class));
		tab4.setIndicator(TAB_ITEM_SETTING).setContent(
				new Intent(this, SettingActivity.class));

		tabHost.addTab(tab1);
		tabHost.addTab(tab2);
		tabHost.addTab(tab3);
		tabHost.addTab(tab4);

		tabHost.setCurrentTab(0);
		RadioButton rb = (RadioButton) group.getChildAt(0);
		rb.setTextColor(getResources().getColor(R.color.white));
		rb.setChecked(true);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, LOGOUT, 0, "注销").setIcon(android.R.drawable.ic_menu_revert);
		menu.add(0, EXIT, 0, "退出").setIcon(
				android.R.drawable.ic_menu_close_clear_cancel);
		menu.add(0, ABOUT, 0, "关于").setIcon(
				android.R.drawable.ic_menu_info_details);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		int id = item.getItemId();
		switch (id) {
		case LOGOUT:
			AlertDialog.Builder aBuilder = new AlertDialog.Builder(this);
			aBuilder.setTitle("用户注销");
			aBuilder.setMessage("确认注销系统，并重新登录?");
			// 确定注销
			aBuilder.setPositiveButton("注销",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							IMPApplication.getInstance().setOpenId("");
							Intent i4 = new Intent(MainActivity.this,
									LoginActivity.class);
							startActivity(i4);
							// sp.edit().putBoolean("remberMe", false).commit();
							MainActivity.this.finish();
						}
					});
			// 取消
			aBuilder.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							return;
						}
					});
			aBuilder.create();
			aBuilder.show();
			break;

		default:
			break;
		}

		
		
		return true;
	}
	

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.radio_button1:
			tabHost.setCurrentTabByTag(TAB_ITEM_HOME);
			break;
		case R.id.radio_button2:
			tabHost.setCurrentTabByTag(TAB_ITEM_BUSINESS);
			break;
		case R.id.radio_button3:
			tabHost.setCurrentTabByTag(TAB_ITEM_APP);
			break;
		case R.id.radio_button4:
			tabHost.setCurrentTabByTag(TAB_ITEM_SETTING);
			break;
		default:
			break;
		}

		((RadioButton) findViewById(R.id.radio_button1))
				.setTextColor(getResources().getColor(R.color.txt_label_gray));
		((RadioButton) findViewById(R.id.radio_button2))
				.setTextColor(getResources().getColor(R.color.txt_label_gray));
		((RadioButton) findViewById(R.id.radio_button3))
				.setTextColor(getResources().getColor(R.color.txt_label_gray));
		((RadioButton) findViewById(R.id.radio_button4))
				.setTextColor(getResources().getColor(R.color.txt_label_gray));
		((RadioButton) findViewById(checkedId)).setTextColor(getResources()
				.getColor(R.color.white));

	};

}
